package com.itljm.cxrMarket.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itljm.cxrMarket.common.PageResult;
import com.itljm.cxrMarket.common.Result;
import com.itljm.cxrMarket.entity.CarouselImages;
import com.itljm.cxrMarket.entity.FeedbackInfo;
import com.itljm.cxrMarket.service.FeedbackInfoService;
import com.itljm.cxrMarket.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author ljm
 * @date 2025/1/2 19:52
 */
@RestController
@RequestMapping("/feedbackInfo")
public class FeedbackInfoController {
    @Autowired
    private FeedbackInfoService feedbackInfoService;

    // 分页查询
    @RequestMapping("/info")
    public PageResult<FeedbackInfo> selectFeedbackInfoByPage(Integer page, Integer limit,
                                                             String feedbackType, String status) {
        if (page == null) {
            page = 0;
        }
        if (limit == null) {
            limit = 15;
        }
        Page<FeedbackInfo> infoPage = new Page<>(page,limit);
        QueryWrapper<FeedbackInfo> wrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(feedbackType)) {
            wrapper.eq("fi.feedback_type", feedbackType);
        }
        if (StringUtils.isNotEmpty(status)) {
            wrapper.eq("fi.status", status);
        }

        List<FeedbackInfo> list = feedbackInfoService.selectFeedbackInfoByPage(infoPage, wrapper);
        return new PageResult<>(list,infoPage.getTotal());
    }

    // 添加或修改反馈信息
    @RequestMapping("/save")
    public Result addOrUpdateFeedbackInfo(@RequestBody FeedbackInfo feedbackInfo) {
        boolean b = feedbackInfoService.saveOrUpdate(feedbackInfo);
        if (b) {
            return Result.ok("操作成功");
        }
        return Result.fail("操作失败");
    }

    // 提交反馈信息
    @RequestMapping("/submit")
    public Result submitFeedbackInfo(@RequestBody FeedbackInfo feedbackInfo) {
        feedbackInfo.setStatus(0);
        boolean b = feedbackInfoService.saveOrUpdate(feedbackInfo);
        if (b) {
            return Result.ok("提交成功");
        }
        return Result.fail("提交失败");
    }

    // 根据用户id查询反馈信息
    @RequestMapping("/infoByUserId")
    public PageResult<FeedbackInfo> selectFeedbackInfoByUserId(Integer page, Integer limit, Long userId) {
        if (page == null) {
            page = 0;
        }
        if (limit == null) {
            limit = 5;
        }
        Page<FeedbackInfo> infoPage = new Page<>(page,limit);
        QueryWrapper<FeedbackInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("fi.user_id",userId);
        wrapper.orderByDesc("fi.created_time");
        List<FeedbackInfo> list = feedbackInfoService.selectFeedbackInfoByPage(infoPage, wrapper);
        return new PageResult<>(list,infoPage.getTotal());
    }

    // 删除反馈信息
    @RequestMapping("/delete/{id}")
    public Result deleteFeedbackInfo(@PathVariable Integer id) {
        boolean b = feedbackInfoService.removeById(id);
        if (b) {
            return Result.ok("删除成功");
        }
        return Result.fail("删除失败");
    }

    // 根据id查询反馈信息
    @RequestMapping("/info/{id}")
    public Result getFeedbackInfoById(@PathVariable Integer id) {
        FeedbackInfo feedbackInfo = feedbackInfoService.getById(id);
        if (feedbackInfo != null) {
            return Result.ok(feedbackInfo);
        }
        return Result.fail("查询失败");
    }
}
